extends Panel

# 技能信息
var skill_id = ""
var skill_name = ""
var key_bind = ""
var cooldown_percent = 0.0

# 冷却计时相关
var is_in_cooldown = false  # 是否处于冷却中，避免错误显示
var cooldown_material = null  # 冷却材质引用

# 更新冷却的函数引用
var update_cooldown_func = null

# 节点引用
@onready var icon_texture = $IconTexture
@onready var key_label = $KeyBackground/KeyLabel
@onready var key_background = $KeyBackground
@onready var cooldown_overlay = $CooldownOverlay
@onready var name_tooltip = $NameTooltip
@onready var name_label = $NameTooltip/Label

# 原始尺寸
var original_size = Vector2(50, 50)

func _ready():
	print("技能格子初始化: ", skill_id)
	
	# 默认隐藏冷却遮罩
	if cooldown_overlay:
		cooldown_overlay.visible = false
		# 保存材质引用
		if cooldown_overlay.material:
			cooldown_material = cooldown_overlay.material
	
	# 隐藏名称提示
	if name_tooltip:
		name_tooltip.visible = false
	
	# 连接鼠标事件
	mouse_entered.connect(_on_mouse_entered)
	mouse_exited.connect(_on_mouse_exited)
	
	# 保存原始尺寸
	original_size = size
	
	# 确保按键提示在冷却遮罩上方显示
	if key_background:
		key_background.z_index = 10
	
	if name_tooltip:
		name_tooltip.z_index = 20
		
	# 确保每个技能格子有唯一的材质实例
	_ensure_unique_material()

# 确保每个技能格子有唯一的材质实例
func _ensure_unique_material():
	if cooldown_overlay and cooldown_overlay.material:
		# 创建材质的副本以避免共享
		cooldown_overlay.material = cooldown_overlay.material.duplicate()
		cooldown_material = cooldown_overlay.material
		print("已为技能", skill_id, "创建唯一材质实例")

# 设置技能信息
func setup(id: String, name: String, key: String, icon_path: String):
	skill_id = id
	skill_name = name
	key_bind = key
	
	print("设置技能格子: ", id, " 快捷键: ", key)
	
	if key_label:
		key_label.text = key
	
	if name_label:
		name_label.text = name
	
	if icon_texture and icon_path:
		var texture = load(icon_path)
		if texture:
			icon_texture.texture = texture
	
	# 确保工具提示在上层显示
	if name_tooltip:
		name_tooltip.z_index = 20
		move_child(name_tooltip, get_child_count() - 1)
	
	# 确保按键背景在上层显示
	if key_background:
		key_background.z_index = 10
		move_child(key_background, get_child_count() - 2)
	
	# 确保冷却遮罩在所有元素下方
	if cooldown_overlay:
		cooldown_overlay.z_index = 1
		move_child(cooldown_overlay, 1)  # 放在图标后面，但在其他元素前面
		
	# 确保每个技能格子有唯一的材质实例
	_ensure_unique_material()
	
	# 初始没有冷却
	set_cooldown(0.0)

# 设置冷却值（0.0到1.0）
func set_cooldown(value: float):
	cooldown_percent = clamp(value, 0.0, 1.0)
	
	# 检测冷却状态变化
	var was_in_cooldown = is_in_cooldown
	is_in_cooldown = cooldown_percent > 0.01
	
	if cooldown_overlay and cooldown_material:
		if is_in_cooldown:  # 只在冷却值大于0.01时显示
			if !was_in_cooldown:
				# 冷却开始，确保遮罩显示
				cooldown_overlay.visible = true
				print("技能 ", skill_id, " 开始冷却")
			
			# 更新冷却进度
			cooldown_material.set_shader_parameter("progress", 1.0 - cooldown_percent)
		else:
			if was_in_cooldown:
				# 冷却结束，隐藏遮罩
				cooldown_overlay.visible = false
				print("技能 ", skill_id, " 冷却结束")
	
	# 更新按键背景颜色，以便在冷却时更醒目
	if key_background:
		if is_in_cooldown:
			# 技能冷却中，使按键背景更亮
			key_background.modulate = Color(1.2, 1.2, 1.2, 1.0)
		else:
			# 正常状态
			key_background.modulate = Color(1.0, 1.0, 1.0, 1.0)

# 每帧更新冷却显示
func _process(_delta):
	if update_cooldown_func != null:
		var cooldown = update_cooldown_func.call()
		set_cooldown(cooldown)

# 鼠标进入时显示技能名称
func _on_mouse_entered():
	if name_tooltip and !name_tooltip.visible:
		# 根据当前尺寸调整提示位置
		var scale_factor = size.x / original_size.x
		name_tooltip.position = Vector2(-25 * scale_factor, -35 * scale_factor)
		name_tooltip.size = Vector2(100 * scale_factor, 30 * scale_factor)
		name_tooltip.visible = true

# 鼠标离开时隐藏技能名称
func _on_mouse_exited():
	if name_tooltip and name_tooltip.visible:
		name_tooltip.visible = false

# 重写尺寸变化方法
func _notification(what):
	if what == NOTIFICATION_RESIZED:
		_update_elements_scale()

# 更新元素缩放
func _update_elements_scale():
	if !is_inside_tree():
		return
		
	var scale_factor = size.x / original_size.x
	
	# 调整内部元素位置和大小
	if icon_texture:
		icon_texture.position = Vector2(4 * scale_factor, 4 * scale_factor)
		icon_texture.size = size - Vector2(8 * scale_factor, 8 * scale_factor)
	
	if cooldown_overlay:
		cooldown_overlay.position = Vector2(4 * scale_factor, 4 * scale_factor)
		cooldown_overlay.size = size - Vector2(8 * scale_factor, 8 * scale_factor)
	
	if key_background:
		key_background.position = Vector2(size.x - 15 * scale_factor, -2 * scale_factor)
		key_background.size = Vector2(17 * scale_factor, 17 * scale_factor)
		# 设置一个较高的Z-索引确保它总是在最上层
		key_background.z_index = 10
		
	if key_label:
		var font_size = int(10 * scale_factor)
		key_label.add_theme_font_size_override("font_size", max(font_size, 8))
		key_label.add_theme_constant_override("outline_size", max(1, int(scale_factor)))
		# 设置更明显的字体颜色和轮廓
		key_label.add_theme_color_override("font_color", Color(1, 1, 1, 1))
		key_label.add_theme_color_override("font_outline_color", Color(0, 0, 0, 1))
	
	if name_tooltip and name_tooltip.visible:
		name_tooltip.position = Vector2(-25 * scale_factor, -35 * scale_factor)
		name_tooltip.size = Vector2(100 * scale_factor, 30 * scale_factor)
		name_tooltip.z_index = 20 